package design_pattern.producer_and_consumer;

import java.util.concurrent.BlockingQueue;

/**
 * @ClassName : design_pattern.producer_and_consumer.Consumer
 * @Description : 类描述
 * Created by cyd on 2021-10-14 10:47:07
 */
public class Consumer implements Runnable {
    // 缓冲区，组赛队列
    private final MyBlockingQueue<String> blockingQueue;

    public Consumer(MyBlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }


    @Override
    public void run() {
        for (int i = 0; i < 100000; i++) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                String result = blockingQueue.take();
                System.out.println("当前消费者线程" +
                        Thread.currentThread().getName() + ",消费产品：" + result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
